//
//  WBNavigationController.swift
//  Weibo
//
//  Created by 夏灵烽 on 2025/1/1.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNaviBarInfo()
    }
     
    // MARK: - 设置导航栏信息
    func setupNaviBarInfo() {
        let appearance = UINavigationBarAppearance()

        // 1> 设置 navBar 整个 背景 的渲染颜色
        appearance.backgroundColor = UIColor.cz_color(withHex: 0xF6F6F6)
        
        // 2> 设置 navBar 标题 的字体颜色
        appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.darkGray]
        appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.darkGray]

        navigationBar.standardAppearance = appearance
        navigationBar.scrollEdgeAppearance = appearance
        
        navigationBar.isTranslucent = false
        
        // 3> 设置系统按钮的文字渲染颜色
        navigationBar.tintColor = .orange
        
    }
    
    // MARK: - 重写push方法，隐藏tabbar，设置导航栏左侧按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            if let vc = viewController as? WBBaseViewController {
                var title = "返回"
                if viewControllers.count == 1 {
                    title = viewControllers.first?.title ?? "返回"
                }                
                vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
            }
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    
    @objc func popToParent() {
        popViewController(animated: true)
    }
}
